package com.ksyun.campus.client;

import java.io.IOException;
import java.io.InputStream;

public class FSInputStream extends InputStream {
    private final byte[] buffer;
    private int position;
    private int count;

    // 构造函数，初始化缓冲区
    public FSInputStream(byte[] data) {
        this.buffer = data;
        this.position = 0;
        this.count = data.length;
    }

    @Override
    public int read() throws IOException {
        if (position < count) {
            return buffer[position++] & 0xFF;  // 返回无符号字节
        } else {
            return -1;  // 到达流的末尾
        }
    }

    @Override
    public int read(byte[] b) throws IOException {
        return read(b, 0, b.length);
    }

    @Override
    public int read(byte[] b, int off, int len) throws IOException {
        if (len < 0 || off < 0 || off + len > b.length) {
            throw new IndexOutOfBoundsException();
        }

        if (position >= count) {
            return -1;  // 到达流的末尾
        }

        int bytesRead = Math.min(len, count - position);
        System.arraycopy(buffer, position, b, off, bytesRead);
        position += bytesRead;
        return bytesRead;  // 返回实际读取的字节数
    }

    @Override
    public void close() throws IOException {

        super.close();
    }
}
